﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HP.Cachalote.DataModels;
using HP.Cachalote.Repositories.Interfaces;

namespace HP.Cachalote.Repositories.EntityFramework
{
    /// <summary>
    /// A factory class that manage every single repository.
    /// </summary>
    public class RepositoryFactory : IRepositoryFactory
    {
        private CachaloteContext _dataContext;

        /// <summary>
        /// Open a database connection without a transaction
        /// </summary>
        public void OpenSession()
        {
            _dataContext = new CachaloteContext();
        }

        /// <summary>
        /// Open a database connection with a transaction
        /// </summary>
        public void OpenSessionWithTransaction()
        {
            OpenSession();
        }

        /// <summary>
        /// Rollback the transaction
        /// </summary>
        public void Rollback() { }

        /// <summary>
        /// Commit the transaction
        /// </summary>
        public void Commit()
        {
            if (_dataContext != null)
            {
                _dataContext.SaveChanges();
            }
        }

        /// <summary>
        /// Flush current data
        /// </summary>
        public void Flush() { }

        /// <summary>
        /// Commit the transaction based on the provided parameter and close the database connection
        /// </summary>
        public void CloseSession()
        {
            Commit();

            if (_dataContext != null)
            {
                _dataContext.Dispose();
            }
        }

        public void Dispose()
        {
            CloseSession();
        }

        /// <summary>
        /// Create an instance of RepositoryBase against the specific DataModel type and provide standard CRUD operation only.
        /// </summary>
        /// <typeparam name="T">The provided DataModel type.</typeparam>
        /// <returns>An instance of RepositoryBase against the specific DataModel type and provide standard CRUD operation only.</returns>
        public IRepository<T> CreateRepositoryBase<T>() where T : DataModelBase
        {
            if (typeof(T) == typeof(TodoLabel))
            {
                return (IRepository<T>)CreateTodoLabelRepository();
            }
            else if (typeof(T) == typeof(TodoItem))
            {
                return (IRepository<T>)CreateTodoItemRepository();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Create an instance of TodoLabelRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of TodoLabelRepository</returns>
        public ITodoLabelRepository CreateTodoLabelRepository()
        {
            return new TodoLabelRepository(_dataContext);
        }

        //// <summary>
        /// Create an instance of TodoItemRepository to do any operation against data source.
        /// </summary>
        /// <returns>An instance of TodoItemRepository</returns>
        public ITodoItemRepository CreateTodoItemRepository()
        {
            return new TodoItemRepository(_dataContext);
        }
    }
}
